package com.gxar.quick.ar.server.base.model.exchange.mapper;

import com.gxar.common.entity.Result;
import com.gxar.quick.ar.server.base.model.exchange.vo.response.*;
import com.gxar.quick.ar.server.base.model.user.exchange.vo.response.UserExchangeItemWorksResponse;
import com.gxar.quick.ar.server.db.model.ExchangeItemEntity;
import com.gxar.quick.ar.server.db.model.ExchangeItemUserEntity;
import com.gxar.quick.ar.server.db.model.WorksEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/2/20 11:10
 */
@Mapper
public interface ExchangeItemMapstruct {

    ExchangeItemMapstruct INSTANCE = Mappers.getMapper(ExchangeItemMapstruct.class);


    @Mapping(target = "userExchangeItemId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getWorksId()))")
    ExchangeItemPageResponse entityToResponse(ExchangeItemUserEntity entity);

    @Mapping(target = "userExchangeItemId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getWorksId()))")
    ExchangeItemInfoResponse entityToInfoResponse(ExchangeItemUserEntity entity);

    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    AdminExchangeItemPageResponse entityToPageResponse(ExchangeItemEntity entity);

    List<AdminExchangeItemPageResponse> entitiesToPageResponses(List<ExchangeItemEntity> exchangeItemEntities);

    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    AdminExchangeItemInfoResponse entityToAdminInfoResponse(ExchangeItemEntity entity);

    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    AdminExchangeItemCreateResponse entityToCreateResponse(ExchangeItemEntity entity);

    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    AdminExchangeItemUpdateResponse entityToUpdateResponse(ExchangeItemEntity entity);

    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "worksSize", expression = "java(getWorkTotalSize(entity))")
    @Mapping(target = "worksName", expression = "java(entity.getName())")
    AdminExchangeItemTemplatePageResponse templateEntityToPageResponse(WorksEntity entity);

    List<AdminExchangeItemTemplatePageResponse> templateEntitiesToPageResponses(List<WorksEntity> worksEntities);

    UserExchangeItemWorksResponse entityToWorks(ExchangeItemEntity exchangeItemEntity);

    default String getWorkTotalSize(WorksEntity entity) {
        int size = entity.getIdentifySize() + entity.getVideoSize() + entity.getU3dModelSize();
        if (size < 1024) {
            return size + "KB";
        } else {
            return BigDecimal.valueOf(size).divide(BigDecimal.valueOf(1024)).setScale(2, RoundingMode.HALF_EVEN) + "MB";
        }
    }
}
